package cn.ffcs.uec.outStaffEntryApply.action;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Tab;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaffEntryApply.action.bean.OutStaffEntryApplyMainBean;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.UomZkUtil;

@Controller
@Scope("prototype")
public class OutStaffEntryApplyMainComposer extends BasePortletComposer
		implements IPortletInfoProvider {

	private static final long serialVersionUID = -5407698728222057185L;

	private OutStaffEntryApplyMainBean bean = new OutStaffEntryApplyMainBean();

	private UecOutStaffInfo uecOutStaffInfo;

	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
		Components.wireVariables(comp, bean);
		bean.getOutStaffEntryApplyListboxExt().setPortletInfoProvider(this);

		bean.getOutStaffEntryApplyListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_INFO_QUERY, comp,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_WORK_QUERY_RESPONSE);

		bean.getOutStaffEntryApplyListboxExt()
				.addForward(
						UecOutStaffConstants.ON_CLEAN_TAB_UEC_OUT_STAFF_INFO_QUERY,
						comp,
						UecOutStaffConstants.ON_CLEAN_TAB_UEC_OUT_STAFF_INFO_QUERY_RESPONSE);

		bean.getUecOutDocEduListboxExt().addForward(
				"onPostEduListOfOutStaffEntry",
				bean.getOutStaffEntryApplyListboxExt(),
				"onPostEduListOfOutStaffEntryResponse");

		bean.getUecOutStaffFamilyListboxExt().addForward(
				"onPostFamilyListOfOutStaffEntry",
				bean.getOutStaffEntryApplyListboxExt(),
				"onPostFamilyListOfOutStaffEntryResponse");

		bean.getUecOutStaffTitleListboxExt().addForward(
				"onPostTitleListOfOutStaffEntry",
				bean.getOutStaffEntryApplyListboxExt(),
				"onPostTitleListOfOutStaffEntryResponse");

		bean.getUecOutStaffNationdutyListboxExt().addForward(
				"onPostNationdutyListOfOutStaffEntry",
				bean.getOutStaffEntryApplyListboxExt(),
				"onPostNationdutyListOfOutStaffEntryResponse");

		bean.getUecOutStaffEncListboxExt().addForward(
				"onPostEncListOfOutStaffEntry",
				bean.getOutStaffEntryApplyListboxExt(),
				"onPostEncListOfOutStaffEntryResponse");

		bean.getUecOutStaffPunishListboxExt().addForward(
				"onPostPunishListOfOutStaffEntry",
				bean.getOutStaffEntryApplyListboxExt(),
				"onPostPunishListOfOutStaffEntryResponse");

		bean.getUecOutStaffPartyLogListboxExt().addForward(
				"onPostPartyLogListOfOutStaffEntry",
				bean.getOutStaffEntryApplyListboxExt(),
				"onPostPartyLogListOfOutStaffEntryResponse");

	}

	/**
	 * 查询到外包员工基本信息的响应处理.
	 * 
	 * @param event
	 */
	public void onUecOutStaffWorkQueryResponse(final ForwardEvent event) {
		uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if (uecOutStaffInfo != null) {
			callTab();
		}
	}

	public void onClickTab(ForwardEvent event) {
		try {
			Event origin = event.getOrigin();
			if (origin != null) {
				Component comp = origin.getTarget();
				if (comp != null && comp instanceof Tab) {
					bean.setTab((Tab) comp);
					callTab();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * tab响应
	 * 
	 * @throws Exception
	 * 
	 */
	public void callTab() {
		try {
			if (bean.getTab() == null) {
				bean.setTab(bean.getTabBox().getSelectedTab());
			}
			if (uecOutStaffInfo != null) {
				String tab = this.bean.getTab().getId();
				Map<String, Object> map = new HashMap<String, Object>();
				boolean isOutStaffEntryExt = true;
				map.put("uecOutStaffInfo", uecOutStaffInfo);
				map.put("isOutStaffEntryExt", isOutStaffEntryExt);
				if ("uecOutStaffDocEdu".equals(tab)) {
					Events.postEvent(
							UecOutStaffConstants.ON_UEC_OUT_DOC_EDU_QUERY_OF_OUTSTAFF_ENTRY,
							this.bean.getUecOutDocEduListboxExt(), map);
				} else if ("uecOutStaffFamily".equals(tab)) {
					Events.postEvent(
							UecOutStaffConstants.ON_UEC_OUT_STAFF_FAMILY_QUERY_OF_OUTSTAFF_ENTRY,
							this.bean.getUecOutStaffFamilyListboxExt(), map);
				} else if ("uecOutStaffTitle".equals(tab)) {
					Events.postEvent(
							UecOutStaffConstants.ON_UEC_OUT_STAFF_TITLE_QUERY_OF_OUTSTAFF_ENTRY,
							this.bean.getUecOutStaffTitleListboxExt(), map);
				} else if ("uecOutStaffNationduty".equals(tab)) {
					Events.postEvent(
							UecOutStaffConstants.ON_UEC_OUT_STAFF_NATION_DUTY_QUERY_OF_OUTSTAFF_ENTRY,
							this.bean.getUecOutStaffNationdutyListboxExt(), map);
				} else if ("uecOutStaffEnc".equals(tab)) {
					Events.postEvent(
							UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY_OF_OUTSTAFF_ENTRY,
							this.bean.getUecOutStaffEncListboxExt(), map);
				} else if ("uecOutStaffPunish".equals(tab)) {
					Events.postEvent(
							UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY_OF_OUTSTAFF_ENTRY,
							this.bean.getUecOutStaffPunishListboxExt(), map);
				} else if ("uecOutStaffPartyLog".equals(tab)) {
					Events.postEvent(
							UecOutStaffConstants.ON_UEC_OUT_STAFF_PARTYLOG_QUERY_OF_OUTSTAFF_ENTRY,
							this.bean.getUecOutStaffPartyLogListboxExt(), map);
				}
			} else {
				// 清空数据
				bean.getUecOutDocEduListboxExt().onCleanUecOutDocEduResponse(
						null);
				bean.getUecOutStaffEncListboxExt()
						.onCleanUecOutStaffEncResponse(null);
				bean.getUecOutStaffPunishListboxExt()
						.onCleanUecOutStaffPunishResponse(null);
				bean.getUecOutStaffPartyLogListboxExt()
						.onCleanUecPartyLogResponse(null);
				bean.getUecOutStaffFamilyListboxExt()
						.onCleanUecOutStaffFamilyResponse(null);
				bean.getUecOutStaffNationdutyListboxExt()
						.onCleanUecOutStaffNationdutyResponse(null);
				bean.getUecOutStaffTitleListboxExt()
						.onCleanUecOutStaffTitleResponse(null);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 清空入职tab响应
	 * 
	 * @param event
	 */
	public void onCleanTabUecStaffInfoQueryResponse(final ForwardEvent event) {
		uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if (uecOutStaffInfo == null) {
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_DOC_EDU,
					bean.getUecOutDocEduListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_ENC,
					bean.getUecOutStaffEncListboxExt(), null);
			Events.postEvent(
					UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PUNISH,
					bean.getUecOutStaffPunishListboxExt(), null);
			Events.postEvent(
					UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PARTYLOG,
					bean.getUecOutStaffPartyLogListboxExt(), null);
			Events.postEvent(
					UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_FAMILY,
					bean.getUecOutStaffFamilyListboxExt(), null);
			Events.postEvent(
					UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_NATION_DUTY,
					bean.getUecOutStaffNationdutyListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TITLE,
					bean.getUecOutStaffTitleListboxExt(), null);
			callTab();
		}
	}
}
